#用户信息表
users=[
    {'name':'Monica','password':'123456','balance':12050.42},
    {'name':'Tony','password':'885546','balance':524000.00},
    {'name':'Richard','password':'987654','balance':1000000000.00},
    {'name':'Erich','password':'111111','balance':64520042.50},
    {'name':'Gilfoyle','password':'888888','balance':23.44}
]

#输出余额
def ShowBalance(i):
    print("您的余额为：￥"+str(users[i]['balance']))

#主循环
login=False  #是否已经登录
id=0  #登录id
while True:
    print("=" * 15, "自动存取款机", "=" * 15)
    if login==False:
        #登录前界面
        print("{0:10} {1:10} {2:10}".format(" ", "1. 登录", "2. 退出"))
        print("=" * 43)
        key = input("请输入对应的服务编号：")
        #根据编号提供相应服务
        if key == '1':
            #登录界面
            print("=" * 19, "登录", "=" * 18)
            flag = True
            name=""
            while True:
                if flag:
                    if name=="":
                        name=input("请输入您的用户名（按q返回）：")
                        if name=='q':
                            break
                        password = input("请输入您的密码：")
                    else:
                        password=input("请重新输入您的密码（按q返回）：")
                        if password=='q':
                            name = ""
                            continue
                    # 检验登录信息
                    for i in range(len(users)):
                        if users[i]['name']==name:
                            if users[i]['password']==password:
                                print("登陆成功！欢迎您，"+name)
                                login=True
                                id=i
                                flag=False
                                break
                            else:
                                print("密码错误，请检查密码！")
                                break
                    else:
                        print("账号不存在，请重新输入！")
                        name=""
                else:
                    break
        elif key == '2':
            #退出
            print("感谢使用，再见！")
            break
    else:
        #登录后界面
        print("{0:8} {1:10} {2:10}".format(" ", "1. 查询账户余额", "2. 取款"))
        print("{0:8} {1:13} {2:10}".format(" ", "3. 存款", "4. 退出登录"))
        print("=" * 43)
        key = input("请输入对应的服务编号：")
        if key == '1':
            #查询余额
            print("=" * 15, "查询账户余额", "=" * 15)
            ShowBalance(id)
            input("按回车键返回……")
        elif key == '2':
            #取款
            print("=" * 19, "取款", "=" * 18)
            while True:
                count=input("请输入取款金额（按q返回）：￥")
                if count=='q':
                    break
                if count.isdigit() and float(count)>0 and (count.find('.')-len(count)<=3 or count.find('.')==-1) and users[id]['balance']-float(count)>=0 :
                    users[id]['balance'] -= float(count)
                    print("取款成功！",end="")
                    ShowBalance(id)
                elif users[id]['balance']-float(count)<0:
                    print("取款失败，余额不足！")
                    ShowBalance(id)
                else:
                    print("取款失败，请输入合法数值！")
        elif key == '3':
            #存款
            print("=" * 19, "存款", "=" * 18)
            while True:
                count=input("请输入存款金额（按q返回）：￥")
                if count=='q':
                    break
                if count.isdigit() and float(count)>0 and (count.find('.')-len(count)<=3 or count.find('.')==-1) :
                    users[id]['balance'] += float(count)
                    print("存款成功！",end="")
                    ShowBalance(id)
                else:
                    print("存款失败，请输入合法数值！")
        elif key == '4':
            # 退出
            login = False #重置登录信息
            id=0
            print("您已成功退出登录！")
